package cn.yj.relay.common.web;

import cn.yj.relay.common.token.AppAuthTokenHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author 永健 
 * @since 2020-01-07 13:12
 */
@RequiredArgsConstructor
public class AuthHandlerInterceptor implements HandlerInterceptor
{
    private static final List<String> NO_AUTH_MAP = new ArrayList<>();

    private final AppAuthTokenHandler tokenHandler;

    static
    {
        /**
         * <br>
         *  登陆的
         */
        NO_AUTH_MAP.add("/login");

        /**
         * 静态文件
         */
        NO_AUTH_MAP.add(".html");
        NO_AUTH_MAP.add(".js");
        NO_AUTH_MAP.add(".css");
        NO_AUTH_MAP.add(".jpg");
        NO_AUTH_MAP.add(".woff");
        NO_AUTH_MAP.add(".png");
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
    {
        String requestURI = request.getRequestURI();

        for (String url : NO_AUTH_MAP)
        {
            if (requestURI.indexOf(url) >= 0)
            {
                return true;
            }
        }
        tokenHandler.checkLoginAuth();
        return true;
    }

}
